# RUN: llc -run-pass livedebugvalues %s -o - | FileCheck %s

# Test if LiveDebugValue analysis works on Wasm.

--- |
  target triple = "wasm32-unknown-unknown"

  define void @test_consts() !dbg !5 {
    call void @llvm.dbg.value(metadata i32 0, metadata !9, metadata !DIExpression()), !dbg !10
    call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !10
    call void @llvm.dbg.value(metadata i32 0, metadata !12, metadata !DIExpression()), !dbg !10
    call void @llvm.dbg.value(metadata i32 0, metadata !13, metadata !DIExpression()), !dbg !10
    ret void
  }

  define void @test_target_indices() !dbg !14 {
    call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !DIExpression()), !dbg !16
    call void @llvm.dbg.value(metadata i32 0, metadata !17, metadata !DIExpression()), !dbg !16
    call void @llvm.dbg.value(metadata i32 0, metadata !18, metadata !DIExpression()), !dbg !16
    call void @llvm.dbg.value(metadata i32 0, metadata !19, metadata !DIExpression()), !dbg !16
    call void @llvm.dbg.value(metadata i32 0, metadata !20, metadata !DIExpression()), !dbg !16
    ret void
  }

  define void @test_target_index_defs() !dbg !21 {
    call void @llvm.dbg.value(metadata i32 0, metadata !22, metadata !DIExpression()), !dbg !23
    call void @llvm.dbg.value(metadata i32 0, metadata !24, metadata !DIExpression()), !dbg !23
    call void @llvm.dbg.value(metadata i32 0, metadata !25, metadata !DIExpression()), !dbg !23
    ret void
  }

  declare void @llvm.dbg.value(metadata, metadata, metadata)

  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!2, !3, !4}

  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
  !1 = !DIFile(filename: "test.c", directory: "")
  !2 = !{i32 7, !"Dwarf Version", i32 5}
  !3 = !{i32 2, !"Debug Info Version", i32 3}
  !4 = !{i32 1, !"wchar_size", i32 4}
  !5 = distinct !DISubprogram(name: "test_consts", scope: !1, file: !1, line: 1, type: !6, scopeLine: 1, unit: !0)
  ; CHECK: ![[T0_SP:[0-9]+]] = distinct !DISubprogram(name: "test_consts"
  !6 = !DISubroutineType(types: !7)
  !7 = !{!8}
  !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  !9 = !DILocalVariable(name: "var0", scope: !5, file: !1, line: 2, type: !8)
  ; CHECK: ![[T0_VAR0:[0-9]+]] = !DILocalVariable(name: "var0", scope: ![[T0_SP]]
  !10 = !DILocation(line: 0, scope: !5)
  !11 = !DILocalVariable(name: "var1", scope: !5, file: !1, line: 2, type: !8)
  ; CHECK: ![[T0_VAR1:[0-9]+]] = !DILocalVariable(name: "var1", scope: ![[T0_SP]]
  !12 = !DILocalVariable(name: "var2", scope: !5, file: !1, line: 2, type: !8)
  ; CHECK: ![[T0_VAR2:[0-9]+]] = !DILocalVariable(name: "var2", scope: ![[T0_SP]]
  !13 = !DILocalVariable(name: "var3", scope: !5, file: !1, line: 2, type: !8)
  ; CHECK: ![[T0_VAR3:[0-9]+]] = !DILocalVariable(name: "var3", scope: ![[T0_SP]]
  !14 = distinct !DISubprogram(name: "test_target_indices", scope: !1, file: !1, line: 10, type: !6, scopeLine: 1, unit: !0)
  ; CHECK: ![[T1_SP:[0-9]+]] = distinct !DISubprogram(name: "test_target_indices"
  !15 = !DILocalVariable(name: "var0", scope: !14, file: !1, line: 11, type: !8)
  ; CHECK: ![[T1_VAR0:[0-9]+]] = !DILocalVariable(name: "var0", scope: ![[T1_SP]]
  !16 = !DILocation(line: 10, scope: !14)
  !17 = !DILocalVariable(name: "var1", scope: !14, file: !1, line: 11, type: !8)
  ; CHECK: ![[T1_VAR1:[0-9]+]] = !DILocalVariable(name: "var1", scope: ![[T1_SP]]
  !18 = !DILocalVariable(name: "var2", scope: !14, file: !1, line: 11, type: !8)
  ; CHECK: ![[T1_VAR2:[0-9]+]] = !DILocalVariable(name: "var2", scope: ![[T1_SP]]
  !19 = !DILocalVariable(name: "var3", scope: !14, file: !1, line: 11, type: !8)
  ; CHECK: ![[T1_VAR3:[0-9]+]] = !DILocalVariable(name: "var3", scope: ![[T1_SP]]
  !20 = !DILocalVariable(name: "var4", scope: !14, file: !1, line: 11, type: !8)
  ; CHECK: ![[T1_VAR4:[0-9]+]] = !DILocalVariable(name: "var4", scope: ![[T1_SP]]
  !21 = distinct !DISubprogram(name: "test_target_index_defs", scope: !1, file: !1, line: 10, type: !6, scopeLine: 1, unit: !0)
  !22 = !DILocalVariable(name: "var0", scope: !21, file: !1, line: 20, type: !8)
  !23 = !DILocation(line: 10, scope: !21)
  !24 = !DILocalVariable(name: "var1", scope: !21, file: !1, line: 20, type: !8)
  !25 = !DILocalVariable(name: "var2", scope: !21, file: !1, line: 20, type: !8)
...

---
# var0: Its debug value is set once in bb.0 and valid throughout the function.
#       So a new DBG_VALUE will be added to the beginnings of all other BBs
#       (bb.1, bb.2, and bb.3).
# var1: Its debug value is set in bb.0, but is reset to no info in bb.1. So a
#       new DBG_VALUE will be added in the beginning of bb.1 and bb.2, but bb.3
#       will not, because its two predecesors (bb.1 and bb.2) have different
#       debug values.
# var2: Its debug value is set both in bb.1 and bb.2 with the same value. bb.3
#       will have a new DBG_VALUE representing that value.
# var3: Its debug value is set both in bb.1 and bb.2 but with different values.
#       bb.3 will NOT have a new DBG_VALUE instruction because its predecessors
#       don't agree on a value.
# CHECK-LABEL: name: test_consts
name: test_consts
liveins:
  - { reg: '$arguments' }
body: |
  bb.0:
    successors: %bb.1, %bb.2
    liveins: $arguments
    DBG_VALUE 10, $noreg, !9, !DIExpression(), debug-location !10
    DBG_VALUE 20, $noreg, !11, !DIExpression(), debug-location !10
    %0:i32 = CONST_I32 1, implicit-def $arguments
    BR_IF %bb.2, %0:i32, implicit-def $arguments

  ; CHECK: bb.1:
  ; CHECK-DAG: DBG_VALUE 10, $noreg, ![[T0_VAR0]]
  ; CHECK-DAG: DBG_VALUE 20, $noreg, ![[T0_VAR1]]
  bb.1:
  ; predecessors: %bb.0
    successors: %bb.3
    DBG_VALUE $noreg, $noreg, !11, !DIExpression(), debug-location !10
    DBG_VALUE 30, $noreg, !12, !DIExpression(), debug-location !10
    DBG_VALUE 30, $noreg, !13, !DIExpression(), debug-location !10
    BR %bb.3, implicit-def $arguments

  ; CHECK: bb.2:
  ; CHECK-DAG: DBG_VALUE 10, $noreg, ![[T0_VAR0]]
  ; CHECK-DAG: DBG_VALUE 20, $noreg, ![[T0_VAR1]]
  bb.2:
  ; predecessors: %bb.0
    successors: %bb.3
    DBG_VALUE 30, $noreg, !12, !DIExpression(), debug-location !10
    DBG_VALUE 40, $noreg, !13, !DIExpression(), debug-location !10
    BR %bb.3, implicit-def $arguments

  ; CHECK: bb.3:
  ; CHECK-DAG: DBG_VALUE 10, $noreg, ![[T0_VAR0]]
  ; CHECK-DAG: DBG_VALUE 30, $noreg, ![[T0_VAR2]]
  ; CHECK-NOT: DBG_VALUE {{[0-9]+}}, $noreg, ![[T0_VAR1]]
  ; CHECK-NOT: DBG_VALUE {{[0-9]+}}, $noreg, ![[T0_VAR3]]
  bb.3:
  ; predecessors: %bb.1, %bb.2
    RETURN implicit-def dead $arguments
...

---
# var0: Its debug value is set once in bb.0 and valid throughout the function.
#       So a new DBG_VALUE will be added to the beginnings of all other BBs
#       (bb.1, bb.2, and bb.3).
# var1: Its debug value is set in bb.0, but is reset to no info back later in
#       bb.0. So no DBG_VALUEs will be added in other BBs.
# var2: Its debug value is set both in bb.1 and bb.2 with the same value (The
#       same kind (local) and same local index). bb.3 will have a new DBG_VALUE
#       representing that value.
# var3: Its debug value is set both in bb.1 and bb.2 but with different values.
#       Both are locals, but their indices are different. bb.3 will NOT have a
#       new DBG_VALUE instruction because its predecessors don't agree on a
#       value.
# var4: Its debug value is set both in bb.1 and bb.2 but with different values.
#       They have different kinds; one is a local and the other is a stack
#       operand. (Their index is the same, but it doesn't matter.) bb.3 will NOT
#       have a new DBG_VALUE instruction because its predecessors don't agree on
#       a value.
# CHECK-LABEL: name: test_target_indices
name: test_target_indices
liveins:
  - { reg: '$arguments' }
body: |
  bb.0:
    successors: %bb.1, %bb.2
    liveins: $arguments
    DBG_VALUE target-index(wasm-local), $noreg, !15, !DIExpression(), debug-location !16
    DBG_VALUE target-index(wasm-operand-stack) + 2, $noreg, !17, !DIExpression(), debug-location !16
    %0:i32 = CONST_I32 1, implicit-def $arguments
    DBG_VALUE $noreg, $noreg, !17, !DIExpression(), debug-location !16
    BR_IF %bb.2, %0:i32, implicit-def $arguments

  ; CHECK: bb.1:
  ; CHECK: DBG_VALUE target-index(wasm-local), $noreg, ![[T1_VAR0]]
  bb.1:
  ; predecessors: %bb.0
    successors: %bb.3
    DBG_VALUE target-index(wasm-local) + 2, $noreg, !18, !DIExpression(), debug-location !16
    DBG_VALUE target-index(wasm-local) + 3, $noreg, !19, !DIExpression(), debug-location !16
    DBG_VALUE target-index(wasm-operand-stack) + 3, $noreg, !20, !DIExpression(), debug-location !16
    BR %bb.3, implicit-def $arguments

  ; CHECK: bb.2:
  ; CHECK: DBG_VALUE target-index(wasm-local), $noreg, ![[T1_VAR0]]
  bb.2:
  ; predecessors: %bb.0
    successors: %bb.3
    DBG_VALUE target-index(wasm-local) + 2, $noreg, !18, !DIExpression(), debug-location !16
    DBG_VALUE target-index(wasm-local) + 5, $noreg, !19, !DIExpression(), debug-location !16
    DBG_VALUE target-index(wasm-local) + 3, $noreg, !20, !DIExpression(), debug-location !16
    BR %bb.3, implicit-def $arguments

  ; CHECK: bb.3:
  ; CHECK-DAG: DBG_VALUE target-index(wasm-local), $noreg, ![[T1_VAR0]]
  ; CHECK-DAG: DBG_VALUE target-index(wasm-local) + 2, $noreg, ![[T1_VAR2]]
  ; CHECK-NOT: DBG_VALUE target-index(wasm-local){{.*}}, $noreg, ![[T1_VAR3]]
  ; CHECK-NOT: DBG_VALUE target-index(wasm-local){{.*}}, $noreg, ![[T1_VAR4]]
  bb.3:
  ; predecessors: %bb.1, %bb.2
    RETURN implicit-def dead $arguments
...

---
# bb.0 defines DBG_VALUEs for local index 2 and 3. The DBG_VALUE for local index
# 2 is killed in bb.1 due to 'local.tee 2' there, and DBG_VALUE for local
# index 3 is killed in bb.2 because there is 'local.set 3' there. As a result,
# bb.3 shoudln't have any additional DBG_VALUEs added for those locals.
# CHECK-LABEL: name: test_target_index_defs
name: test_target_index_defs
liveins:
  - { reg: '$arguments' }
body: |
  bb.0:
    successors: %bb.1, %bb.2
    liveins: $arguments
    DBG_VALUE target-index(wasm-local) + 2, $noreg, !22, !DIExpression(), debug-location !23
    DBG_VALUE target-index(wasm-local) + 3, $noreg, !24, !DIExpression(), debug-location !23
    %0:i32 = CONST_I32 1, implicit-def $arguments
    BR_IF %bb.2, %0:i32, implicit-def $arguments

  bb.1:
  ; predecessors: %bb.0
    successors: %bb.3
    %0:i32 = CONST_I32 1, implicit-def $arguments
    %1:i32 = LOCAL_TEE_I32 2, %0:i32, implicit-def $arguments
    DROP_I32 killed %1:i32, implicit-def $arguments
    BR %bb.3, implicit-def $arguments

  bb.2:
  ; predecessors: %bb.0
    successors: %bb.3
    %0:i32 = CONST_I32 1, implicit-def $arguments
    LOCAL_SET_I32 3, %0:i32, implicit-def $arguments
    BR %bb.3, implicit-def $arguments

  ; CHECK: bb.3:
  ; CHECK-NOT: DBG_VALUE target-index(wasm-local){{.*}}
  bb.3:
  ; predecessors: %bb.1, %bb.2
    RETURN implicit-def dead $arguments
...
